﻿//Copyright (c) Microsoft Corporation. All rights reserved.

using Microsoft.ApplicationServer.Caching;
using Microsoft.Ted.Wacel.TestConsole.Properties;
using Microsoft.Ted.Wacel;
using Microsoft.Ted.Wacel.Cache;
using System;

namespace Microsoft.Ted.Wacel.TestConsole
{
    public class ArrayTester
    {
        /// <summary>
        /// Test easiest case to write and read an array item.
        /// </summary>
        public static void SimpleAccess()
        {
            Array<double> array = new Array<double>("myarray", 1000, Settings.Default.StorageConnectionString);
            array[100] = 123.45;
            VisualTester.AreEqual(123.45, array[100]);
        }
        /// <summary>
        /// This test case simulates a cache cluster crash. Requested item should be seamlessly served from
        /// the table storage in this case.
        /// </summary>
        public static void Failover()
        {
            Array<double> array = new Array<double>("myarray", 1000, Settings.Default.StorageConnectionString);
            array[100] = 456.78;

            //Simulate a cache cluster crash by clearing the cache
            WindowsAzureCacheDataProvider provider = new WindowsAzureCacheDataProvider(new DataCache(), "myarray")
                { Configuration = new WindowsAzureCacheConfiguration() };
            provider.Clear();

            //Make sure the cache has been cleared
            VisualTester.IsNull(provider.Get("100"));

            //Data is retrieved from table
            VisualTester.AreEqual(456.78, array[100]);

            //Now manually update the cache
            provider.Put("100", 789.00);

            //Data is retrieved from the cache
            VisualTester.AreEqual(789.00, array[100]);

            //Update array normally
            array[100] = 101.23;

            //Now the data is consistent again
            VisualTester.AreEqual(101.23, array[100]);
        }
    }
}
